<?php

include_once 'dbtable.php';

//lay danh sach thanh vien
function get_all_members(pager $page, $url) {
    $sql = "select * from user order by id desc";
    return DbTable::Paging($page, $sql, $url,30);
}
function get_members_by_id($id) {
    $sql = "select * from user where id='{$id}' order by id desc";
    return DbTable::getData($sql);
}
//lay danh asch ra trang frontpage
function get_member_to_frontpage() {
    $sql = "select * from user order by fullname asc";
    return DbTable::getData($sql);
}
//lay danh sach thanh vien theo ten
function get_all_members_by_name(pager $page, $url, $name) {
    $name = mysql_real_escape_string($name);
    $sql = "select * from user where fullname like '%" . $name . "%' order by id desc";
    return DbTable::Paging($page, $sql, $url,15);
}

function get_members_by_status(pager $page, $url, $stt) {
    $stt = mysql_real_escape_string($stt);
    $sql = "select * from user where status = '{$stt}' order by id desc";
    return DbTable::Paging($page, $sql, $url,15);
}

//them moi thanh vien
function them_moi_thanhvien($array) {

    if (DbTable::insert("user", $array)) {
        return true;
    }
    return false;
}

//sua thong tin thanh vien
function sua_thanhvien($array, $id) {
    if (DbTable::update("user", $array, $id)) {
        return true;
    }
    return false;
}

//xoa thong tin thanh vien
function xoa_thanhvien($id) {
    if (DbTable::delete("user", $id)) {
        return true;
    }
    return false;
}
function get_masv($masv) {
    $sql = "select masv from user where masv = '{$masv}'";
    return DbTable::getData($sql);
}
function get_username($username) {
    $sql = "select username from user where username = '{$masv}'";
    return DbTable::getData($sql);
}
//thay doi trang thai thanh vien
function thay_doi_trang_thai_thanhvien($status, $id) {

    $status == 1 ? $status = 0 : $status = 1;

    if (mysql_query("update user set status='{$status}' where id = '{$id}'")) {
        return true;
    }
    return false;
}

//dang nhap he thong
function login_system($username, $password) {
    $username = mysql_real_escape_string($username);
    $password = md5(mysql_real_escape_string($password));

    $sql = "select * from user where username = '{$username}' and password='{$password}' and status = 1";
        
    return DbTable::getData($sql);
}

//check name
function checkUsername($name) {
    $sql = "select * from user where username='{$name}'";
    $result = mysql_query($sql);
    if (mysql_num_rows($result) > 0) {
        return false;
    }
    else
        return true;
}
//lay thong tin thanh vien de sua
function get_thong_tin_thanhvien($id) {
    $sql = "select * from user where id = '{$id}'";
    return DbTable::First($sql);
}
//lay thong tin thanh vien de sua1
function get_thong_tin_thanhvien_1($id) {
    $sql = "select * from user where id = '{$id}'";
    return DbTable::getData($sql);
}
function checkPass($username,$password){
	$password = md5(mysql_real_escape_string($password));
	$sql = "select * from user where username = '{$username}' and password='{$password}' and status = 1";	
	return DbTable::getData($sql);
}
//tim kiem usser theo ajax
function search_form_ajax_user($keyword) {
    $sql = "select * from user where fullname like '%{$keyword}%' order by fullname asc limit 0,6";
    $list = DbTable::getData($sql);
    foreach($list as $r){
        echo "<li>";
            echo '<div style="float: left;">';
                echo "<img class='img-border' src='data/avatar/{$r["avatar"]}' width='40px' height='40px' alt='default.png'/>";
            echo "</div>";
            echo '<div style="float:left;width:180px;word-wrap:break-word;padding-left:3px;">';
                echo "<b><a class='link' style='padding-left:0px;color:#204eb7;font-size: 1.1em' href='member.php?uid={$r["id"]}'>{$r["fullname"]}</a></b>";                                        
                echo "<br/>";
                echo "<p>";
                    echo $r["quequan"];
                echo "</p>";
            echo "</div>";
            echo "<div style='clear: both'></div>";
        echo "</li>";
    }
}
?>
